/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.chigs.web.common.modal;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.feedback.ContainerFeedbackMessageFilter;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.googlecode.chigs.web.common.IconFactory;
import com.googlecode.chigs.web.common.forms.ShinyForm;

/**
 * 
 * @author Ingo Renner
 * @author Henning Teek
 *
 * @param <T>
 */
public abstract class AModalWindowContentPanel<T>  extends Panel {
    private static Logger log =  LoggerFactory.getLogger(AModalWindowContentPanel.class);

    private FeedbackPanel feedbackPanel;

	private IModel<T> model;

	public AModalWindowContentPanel(String id, IModel<T> model) {
		super(id);
		this.model = model;
	}

	@Override
	public void onBeforeRender() {
		if (!hasBeenRendered()){
			add(new InnerForm("form", getModel()));
		}
		super.onBeforeRender();
	}
	
	private IModel<T> getModel() {
		return model;
	}

	public void setModel(IModel<T> model) {
		this.model = model;
	}

	protected abstract  void afterSubmit(AjaxRequestTarget target, IModel<T> model);

	protected abstract  void afterCanel(AjaxRequestTarget target);
	
    protected abstract Component fillForm(String componentId, IModel<T> model);

	class InnerForm extends ShinyForm<T> {		

		public InnerForm(String id, IModel<T> model) {			
			super(id, model);
			feedbackPanel = new FeedbackPanel("feedback", new ContainerFeedbackMessageFilter(this));
			feedbackPanel.setOutputMarkupId(true);
            add(feedbackPanel); 			
			AjaxSubmitLink submitLink = new AjaxSubmitLink("submitButton") {
				@Override
				protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
					AModalWindowContentPanel.this.afterSubmit(target, InnerForm.this.getModel());
				}
				@Override
				protected void onError(AjaxRequestTarget target, Form<?> form) {
					target.addComponent(feedbackPanel);
				}
			};
			submitLink.add(new Image("submit", IconFactory.createOkIconReference()));
			add(submitLink);
			AjaxLink<Void> cancelLink = new AjaxLink<Void>("cancelButton") {
				@Override
				public void onClick(AjaxRequestTarget target) {
					AModalWindowContentPanel.this.afterCanel(target);
				}
			};
			cancelLink.add(new Image("icon", IconFactory.createCancelIconReference()));
			add(cancelLink);
		}
		
		@Override
		protected boolean allowShinyOnAjaxRequest() {
			return true;
		}

		@Override
		public void onBeforeRender() {
			if (!hasBeenRendered()){
				Component c = AModalWindowContentPanel.this.fillForm("formcontent", getModel());
				c.setOutputMarkupPlaceholderTag(false);
				add(c);
			}
			super.onBeforeRender();
		}		
	}
}
